<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket</title>
    <link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <script src="/webjars/jquery/jquery.min.js"></script>
    <script src="/webjars/sockjs-client/sockjs.min.js"></script>
    <script src="/webjars/stomp-websocket/stomp.min.js"></script>
</head>
<body>
<div>
    <div>
        <label for="url">连接地址</label>
        <input type="text" value="http://localhost:8080/websocket-simple" id="url" placeholder="请输入连接地址"/>
        <button id="connect" onclick="connect();">连接</button>
        <button id="disconnect" disabled="disabled" onclick="disconnect();">断开连接</button>
    </div>

    <div id="conversationDiv">
        <label for="name">输入你的名字</label><input type="text" id="name" placeholder="请输入姓名"/>
        <button id="sendName" onclick="sendName();">发送</button>
        <p id="response"></p>
        <p id="callback"></p>
        <div id="ggyro">
            <div class="map1">
                <ul>
                    <li>X: <span class="x"></span></li>
                    <li>Y: <span class="y"></span></li>
                    <li>Z: <span class="z"></span></li>
                    <li>xScaled: <span class="xScaled"></span></li>
                    <li>yScaled: <span class="yScaled"></span></li>
                    <li>zScaled: <span class="zScaled"></span></li>
                    <li>xRotation: <span class="xRotation"></span></li>
                    <li>yRotation: <span class="yRotation"></span></li>
                </ul>
            </div>
            <div class="map2">
                <ul>
                    <li>X: <span class="x"></span></li>
                    <li>Y: <span class="y"></span></li>
                    <li>Z: <span class="z"></span></li>
                    <li>xScaled: <span class="xScaled"></span></li>
                    <li>yScaled: <span class="yScaled"></span></li>
                    <li>zScaled: <span class="zScaled"></span></li>
                    <li>xRotation: <span class="xRotation"></span></li>
                    <li>yRotation: <span class="yRotation"></span></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var stompClient = null;

    function setConnected(connected) {
        document.getElementById("connect").disabled = connected;
        document.getElementById("disconnect").disabled = !connected;
        document.getElementById("conversationDiv").style.visibility = connected ? 'visible' : 'hidden';
        $("#response").html();
        $("#callback").html();
        $("#ggyro").html();
    }

    function connect() {
        var socket = new SockJS($("#url").val());
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function (frame) {
            setConnected(true);
            console.log('Connected:' + frame);
            stompClient.subscribe('/topic/recv', function (response) {
                showResponse(response.body);
            });
            // 另外再注册一下定时任务接受
            stompClient.subscribe('/topic/callback', function (response) {
                showCallback(response.body);
            });
            // 另外再注册一下定时任务接受
            stompClient.subscribe('/topic/ggyro', function (response) {
                // $("#ggyro").html(response.body);
                var obj = JSON.parse(response.body);
                var show = function (jq, json) {
                    $(jq).find(".x").html(json['x']);
                    $(jq).find(".y").html(json['y']);
                    $(jq).find(".z").html(json['z']);

                    $(jq).find(".xScaled").html(json['xScaled']);
                    $(jq).find(".yScaled").html(json['yScaled']);
                    $(jq).find(".zScaled").html(json['zScaled']);

                    $(jq).find(".xRotation").html(json['xRotation']);
                    $(jq).find(".yRotation").html(json['yRotation']);
                };
                show($("#ggyro .map1"), obj['map1']);
                show($("#ggyro .map2"), obj['map2']);
            });
        });
    }

    function disconnect() {
        if (stompClient != null) {
            stompClient.disconnect();
        }
        setConnected(false);
        console.log('Disconnected');
    }

    function sendName() {
        var name = $('#name').val();
        console.log('name:' + name);
        stompClient.send("/send", {}, JSON.stringify({'name': name}));
    }

    function showResponse(message) {
        $("#response").html(message);
    }

    function showCallback(message) {
        $("#callback").html(message);
    }
</script>

</body>
</html>